<?php
/**
 * 贷款咨询模型
 */

namespace app\badmin\model;


use app\common\model\BaseModel;
use think\Db;

class CompanyLoanConsult extends BaseModel {

    /**
     * 婚姻状况
     * @var array
     */
    public static $marriageType = [
        0 => '未知',
        1 => '未婚',
        2 => '已婚',
    ];

    /**
     * 学历
     * @var array
     */
    public static $educationType = [
        1 => '中专或高中',
        2 => '大专',
        3 => '本科',
        4 => '研究生及以上',
    ];

    /**
     * 用户贷款需求
     * @param $params
     * @return array|\PDOStatement|string|\think\Model|null
     */
    public static function companyLoanConsult($params) {
        $column = ['money,marriage_type,education_type,company,post,month_money,number,loan_money,balance_money,car_money,guarantee_money,nation,remark'];
        $user_id = Db::name("company_agent_user")->where('id', $params['id'])->value('user_id');
        $map[] = ['user_id', '=', $user_id];
        $data = self::getOne($map, $column);
        if (empty($data)) {
            $data = [];
        } else {
            $data['money'] = deal_data_info($data['money'], 2);
            $data['marriage_type'] = self::$marriageType[$data['marriage_type']];
            $data['education_type'] = self::$educationType[$data['education_type']];
            $data['month_money'] = deal_data_info($data['month_money'], 2);
            $data['loan_money'] = deal_data_info($data['loan_money'], 2);
            $data['balance_money'] = deal_data_info($data['balance_money'], 2);
            $data['car_money'] = deal_data_info($data['car_money'], 2);
            $data['guarantee_money'] = deal_data_info($data['guarantee_money'], 2);
        }
        return $data;
    }

    /**
     * 获取单条
     * @param $map
     * @param $column
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getOne($map, $column) {
        return db("company_loan_consult")
            ->where($map)
            ->field($column)
            ->find();
    }

}